from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession

from module_admin.entity.do.kh_monthly_attendance_do import KhMonthlyAttendance
from module_admin.entity.vo.kh_monthly_attendance_vo import Kh_monthly_attendancePageQueryModel, \
    Kh_monthly_attendanceModel
from utils.page_util import PageUtil


class Kh_monthly_attendanceDao:
    """
    考勤月报表模块数据库操作层
    """

    @classmethod
    async def get_kh_monthly_attendance_detail_by_id(cls, db: AsyncSession, id: int):
        """
        根据序号获取考勤月报表详细信息

        :param db: orm对象
        :param id: 序号
        :return: 考勤月报表信息对象
        """
        kh_monthly_attendance_info = (
            (
                await db.execute(
                    select(KhMonthlyAttendance)
                    .where(
                        KhMonthlyAttendance.id == id
                    )
                )
            )
            .scalars()
            .first()
        )

        return kh_monthly_attendance_info

    @classmethod
    async def get_kh_monthly_attendance_detail_by_info(cls, db: AsyncSession, kh_monthly_attendance: Kh_monthly_attendanceModel):
        """
        根据考勤月报表参数获取考勤月报表信息

        :param db: orm对象
        :param kh_monthly_attendance: 考勤月报表参数对象
        :return: 考勤月报表信息对象
        """
        kh_monthly_attendance_info = (
            (
                await db.execute(
                    select(KhMonthlyAttendance).where(
                        KhMonthlyAttendance.user_name == kh_monthly_attendance.user_name if kh_monthly_attendance.user_name else True,
                        KhMonthlyAttendance.user_id == kh_monthly_attendance.user_id if kh_monthly_attendance.user_id else True,
                        KhMonthlyAttendance.kh_month == kh_monthly_attendance.kh_month if kh_monthly_attendance.kh_month else True,
                        KhMonthlyAttendance.am_pm == kh_monthly_attendance.am_pm if kh_monthly_attendance.am_pm else True,
                        KhMonthlyAttendance.kh_work == kh_monthly_attendance.kh_work if kh_monthly_attendance.kh_work else True,
                    )
                )
            )
            .scalars()
            .first()
        )

        return kh_monthly_attendance_info

    @classmethod
    async def get_kh_monthly_attendance_list(cls, db: AsyncSession, query_object: Kh_monthly_attendancePageQueryModel, is_page: bool = False):
        """
        根据查询参数获取考勤月报表列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 考勤月报表列表信息对象
        """
        query = (
            select(KhMonthlyAttendance)
            .where(
                KhMonthlyAttendance.user_name.like(f'%{query_object.user_name}%') if query_object.user_name else True,
                KhMonthlyAttendance.user_id == query_object.user_id if query_object.user_id else True,
                KhMonthlyAttendance.kh_month == query_object.kh_month if query_object.kh_month else True,
                KhMonthlyAttendance.am_pm == query_object.am_pm if query_object.am_pm else True,
                KhMonthlyAttendance.kh_work == query_object.kh_work if query_object.kh_work else True,
                KhMonthlyAttendance.day1 == query_object.day1 if query_object.day1 else True,
                KhMonthlyAttendance.day2 == query_object.day2 if query_object.day2 else True,
                KhMonthlyAttendance.day3 == query_object.day3 if query_object.day3 else True,
                KhMonthlyAttendance.day4 == query_object.day4 if query_object.day4 else True,
                KhMonthlyAttendance.day5 == query_object.day5 if query_object.day5 else True,
                KhMonthlyAttendance.day6 == query_object.day6 if query_object.day6 else True,
                KhMonthlyAttendance.day7 == query_object.day7 if query_object.day7 else True,
                KhMonthlyAttendance.day8 == query_object.day8 if query_object.day8 else True,
                KhMonthlyAttendance.day9 == query_object.day9 if query_object.day9 else True,
                KhMonthlyAttendance.day10 == query_object.day10 if query_object.day10 else True,
                KhMonthlyAttendance.day11 == query_object.day11 if query_object.day11 else True,
                KhMonthlyAttendance.day12 == query_object.day12 if query_object.day12 else True,
                KhMonthlyAttendance.day13 == query_object.day13 if query_object.day13 else True,
                KhMonthlyAttendance.day14 == query_object.day14 if query_object.day14 else True,
                KhMonthlyAttendance.day15 == query_object.day15 if query_object.day15 else True,
                KhMonthlyAttendance.day16 == query_object.day16 if query_object.day16 else True,
                KhMonthlyAttendance.day17 == query_object.day17 if query_object.day17 else True,
                KhMonthlyAttendance.day18 == query_object.day18 if query_object.day18 else True,
                KhMonthlyAttendance.day19 == query_object.day19 if query_object.day19 else True,
                KhMonthlyAttendance.day20 == query_object.day20 if query_object.day20 else True,
                KhMonthlyAttendance.day21 == query_object.day21 if query_object.day21 else True,
                KhMonthlyAttendance.day22 == query_object.day22 if query_object.day22 else True,
                KhMonthlyAttendance.day23 == query_object.day23 if query_object.day23 else True,
                KhMonthlyAttendance.day24 == query_object.day24 if query_object.day24 else True,
                KhMonthlyAttendance.day25 == query_object.day25 if query_object.day25 else True,
                KhMonthlyAttendance.day26 == query_object.day26 if query_object.day26 else True,
                KhMonthlyAttendance.day27 == query_object.day27 if query_object.day27 else True,
                KhMonthlyAttendance.day28 == query_object.day28 if query_object.day28 else True,
                KhMonthlyAttendance.day29 == query_object.day29 if query_object.day29 else True,
                KhMonthlyAttendance.day30 == query_object.day30 if query_object.day30 else True,
                KhMonthlyAttendance.day31 == query_object.day31 if query_object.day31 else True,
            )
            .order_by(KhMonthlyAttendance.id)
            .distinct()
        )
        kh_monthly_attendance_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return kh_monthly_attendance_list

    @classmethod
    async def add_kh_monthly_attendance_dao(cls, db: AsyncSession, kh_monthly_attendance: Kh_monthly_attendanceModel):
        """
        新增考勤月报表数据库操作

        :param db: orm对象
        :param kh_monthly_attendance: 考勤月报表对象
        :return:
        """
        db_kh_monthly_attendance = KhMonthlyAttendance(**kh_monthly_attendance.model_dump(exclude={}))
        db.add(db_kh_monthly_attendance)
        await db.flush()

        return db_kh_monthly_attendance

    @classmethod
    async def edit_kh_monthly_attendance_dao(cls, db: AsyncSession, kh_monthly_attendance: dict):
        """
        编辑考勤月报表数据库操作

        :param db: orm对象
        :param kh_monthly_attendance: 需要更新的考勤月报表字典
        :return:
        """
        await db.execute(update(KhMonthlyAttendance), [kh_monthly_attendance])

    @classmethod
    async def delete_kh_monthly_attendance_dao(cls, db: AsyncSession, kh_monthly_attendance: Kh_monthly_attendanceModel):
        """
        删除考勤月报表数据库操作

        :param db: orm对象
        :param kh_monthly_attendance: 考勤月报表对象
        :return:
        """
        await db.execute(delete(KhMonthlyAttendance).where(KhMonthlyAttendance.id.in_([kh_monthly_attendance.id])))

